#ifndef SONGITEM_H
#define SONGITEM_H

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QPixmap>
#include <QImage>
#include <QFile>
#include <QBuffer>
#include <QPainter>
#include <QPainterPath>
#include <QTime>
#include <QJsonObject>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QMediaMetaData>

#include <QThread>

#include <titlebar.h>


/*
 *
 *
class SongItem : public QWidget
{
public:
    explicit SongItem(QWidget *parent = nullptr);
    void SetChosen();设置被选中
    void SetNotChosen();设置没被选中
    void SetSongers(const QStringList&songers)设置歌手
    void SetAlbum(const QString&albumname)设置专辑
    void SetSongName(const QString&songname)歌名
    void SetOrder(qint32 order)设置歌曲序号
    void SetDuration(qint32 m,qint32 s)设置歌曲时长
    void SetPicture(const QByteArray&bytearray);下面是三种设置图片的方法
    void SetPicture(const QString&filepath);
    void SetPicture(const QImage&image);
    void SetLike();设置喜欢
    void SetNormal();设置为正常
    void SetCheckMode(bool flag)true开启复选模式，以便批量操作
    bool isChecked()复选框是否被点击
    static QPixmap paintPictureRoundRect(const QPixmap&pixmap, qint32 radius);将图片QPixmap设置圆角的静态函数

signals:
    void mouseRightPress(const QPoint& mousepos);鼠标右键点击时信号（要出现小窗口）
    void chosen();当一个SongItem选中时信号
    void doubleClicked();双击SongItem播放歌曲，同时会有chosen信号产生
    void clickLike();取消喜欢时
    void clickNormal();点击喜欢时
};

class SongItemFirst: public QWidget这个类是SongItem列的首项，不需要对它做任何事情，只需在QVBoxLayout中在SongItem前面即可

 *
 *
 *
 *
*/

class AdaptiveMultiLabel:public AdaptiveLabel
{
    Q_OBJECT
public:
    AdaptiveMultiLabel(QWidget *parent=nullptr):AdaptiveLabel(parent){}
    void SetStringList(const QStringList&strlist);
    void setAdaptiveText(const QString&text){AdaptiveLabel::setAdaptiveText(text);}
    void SetAdaptiveSize(){QFontMetrics metrics(font()); auto rect =metrics.boundingRect(text());setFixedSize(rect.width()+5,rect.height()+5);}
    const QString adaptiveText()const {return AdaptiveLabel::adaptiveText();}
    void setGeometry(qint32 x,qint32 y,qint32 width,qint32 height);

protected:
    void mousePressEvent(QMouseEvent*e)override;

private:
    QString FindString(const QPointF&pointf);
    QStringList mstrlist;
};

class SongItem : public QWidget
{
    Q_OBJECT
public:
    explicit SongItem(QWidget *parent = nullptr);
    void SetChosen();//设置被选中
    void SetNotChosen();
    void SetSongers(const QStringList&songers){songerslabel->SetStringList(songers);}
    void SetAlbum(const QString&albumname){albumlabel->setAdaptiveText(albumname);}
    void SetSongName(const QString&songname){titlelabel->setAdaptiveText(songname);}
    void SetOrder(qint32 order){if(order<10)orderlabel->setText("0"+QString::number(order));else orderlabel->setText(QString::number(order));}
    void SetDuration(qint32 m,qint32 s){QTime time(0,m,s);durationlabel->setText(time.toString("mm:ss"));}
    void SetTime(const QTime&time){durationlabel->setText(time.toString("hh:mm:ss"));}
    void SetPicture(const QByteArray&bytearray);
    void SetPicture(const QString&filepath);
    void SetPicture(const QImage&image);
    void SetLikeMode(bool flag);
    void SetLike();
    void SetNormal();
    void SetCheckMode(bool flag);
    void SetCheck(bool flag);
    void SetPlayMode(bool flag);
    void SetSongId(qint64 id){songid=id;}
    QString Title(){return titlelabel->text();}
    QString AlbumName(){return albumlabel->text();}
    QString Songers(){return songerslabel->text();}
    qint64 id(){return songid;}
    QPixmap Picture(){return picturelabel->pixmap();}
    bool isEmptyUrl(){return mp3filepath.isEmpty();}
    bool isChecked(){if(checkbox->isHidden())return false;return true;}
    bool isNetFile(){ return !mp3filepath.isLocalFile();}
    QString FilePathStr(){return mp3filepath.toString();}
    QUrl FilePathUrl(){return mp3filepath;}
    static QPixmap paintPictureRoundRect(const QPixmap&pixmap, qint32 radius);
    void SetSource(const QUrl&url);
    static SongItem* FromLocalFile(const QString&filepath);
    static SongItem* FromJsonOBJect(const QJsonObject&obj);
    static SongItem* FromUrl(const QUrl&url);

signals:
    void updateAlbumPicture(const QImage&image);
    void mouseRightPress(const QPoint& mousepos);
    void chosen();
    void doubleClicked();
    void dislike();
    void like();
    void metaDataReady();
protected:
    void paintEvent(QPaintEvent*e)override;
    void mousePressEvent(QMouseEvent*e)override;
    void resizeEvent(QResizeEvent*e)override;
    bool event(QEvent*e)override;//鼠标悬浮
    void onButtonClicked();
private slots:
    void onMetaDataChanged();
private:

    AdaptiveMultiLabel*songerslabel,*albumlabel,*titlelabel;
    QLabel*orderlabel,*durationlabel,*picturelabel;
    QPushButton*likebtn;
    QColor color;
    QCheckBox*checkbox;
    QMediaPlayer*player;

    bool ischosen=false;
    bool islike=false;
    const qint32 fixedwidth;
    qint64 songid;
    qint64 lastclicktime;
    QUrl mp3filepath;
    QTime clicktime;
};

class SongItemFirst: public QWidget
{
    Q_OBJECT
public:
    SongItemFirst(QWidget*parent=nullptr);
    void SetCheckMode(bool flag);
    void SetCheck(bool flag){if(flag)checkbox->setCheckState(Qt::Checked); else checkbox->setCheckState(Qt::Unchecked);}
    void SetLikeMode(bool flag);
    void SetDurationText(const QString&text){durationlabel->setText(text);}
signals:
    void selectAll(bool flag);
protected:
    void resizeEvent(QResizeEvent*e)override;
private:
    QLabel*orderlabel,*titlelabel,*albumlabel,*likelabel,*durationlabel;
    QHBoxLayout*mainlayout;
    const qint32 fixedwidth;
    QCheckBox *checkbox;
};


#endif // SONGITEM_H
